<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 树型显示 -->
        <el-tree
          :data="list"
          default-expand-all
        >
          <!-- 作用域插槽获取数据 -->
          <template #default="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length===0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <!-- 弹出1文本框组件 -->
      <!-- title="新增或编辑"改成动态的弹层标题 -->
      <el-dialog
        :title="isEdit ? '编辑':'添加'"
        :visible.sync="showDialog"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
      >
        <!-- 多个组件用到弹层，在cre。。里面调用，弹层只能显示第一次打开的内容，后面打开都是第一次打开的内容
      1，可以用v-if="showDialog" 变量true、false来控制
      2利用$nextTick跟ref来实现同步更新ref="myAddRef
        // 方法三 watch事件监听"-->
        <addOrEdit v-if="showDialog" :id="curId" :is-edit="isEdit" :origin-list="originList" :close="hClose" @close="showDialog=false" @success="hSuccess" />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import addOrEdit from './deptDialog.vue'// 导入子组件
import { getDepartments, delDepartments } from '@/api/department'
import { tranDataToTree } from '@/utils'// 导入自己封装的函数
export default {
  components: {
    addOrEdit
  },
  data() {
    return {
      originList: [],
      isEdit: false,
      curId: '', // 当前点击部门的id
      showDialog: false,
      list: []// 保存数据渲染
    }
  },
  created() {
    this.loadDepartment()
  },
  methods: {
    hClose() {
      // 重置表单,清空校验信息
      this.$refs.myAddRef.reseForm()// 这个是对$nextTick来用的
    },
    // 删除部门功能
    hDel(id) {
      this.$confirm('您确定要删除吗？？').then(
        async() => {
          const res = await delDepartments(id)
          if (res.code !== 10000) return this.$message.error(res.message)
          this.$message.success(res.message)
          // 重新渲染页面
          this.loadDepartment()
        }
      ).catch(err => err)
    },
    // 子传父接受数据，进行渲染，然后关闭弹层
    hSuccess() {
      this.loadDepartment()// 调用、渲染数据
      this.showDialog = false// 关闭弹层
    },
    hAdd(id) { // 将上面的点击显示，改成一个函数，然后再用父传子，把id传过去
      this.curId = id
      this.isEdit = false
      this.showDialog = true
    },
    hEdit(id) {
      this.curId = id
      this.isEdit = true
      this.showDialog = true
      // 2利用$nextTick跟ref来实现同步更新
      // this.$nextTick(() => {
      //   this.$refs.myAddRef.loadDepartmentDetail()
      // })
    },
    // 获取数据
    async loadDepartment() {
      try {
        const res = await getDepartments()
        console.log(res)
        res.data.depts.shift()// 删除数组第一个数据
        this.originList = res.data.depts.map(item => {
          return { id: item.id, pid: item.pid, code: item.code, name: item.name }
        })
        // tranDataToTree(res.data.depts)调用数据平铺数据实现树型结构
        this.list = tranDataToTree(res.data.depts)// 直接渲染好,交给list去渲染
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
